from django.views import View

from ihome.utils.fastdfs.fastdfs_storage import FastDFSStorage
from wangziye.models import HouseImage, House, Order
from django.http import JsonResponse

class Home_Page(View):
    #首页房屋推荐

    def get(self,request):
        #数据库取出数据
        try:
            channels = House.objects.order_by('id')
        except Exception as e:
            return JsonResponse({"errno":"4001",
                                 "errmsg":"数据库查询错误"})
        fdfs = FastDFSStorage()

        #数据库遍历出data数据放入列表
        home_list = []
        num = 0
        for channel in channels:
            num += 1
            home_list.append(
                {
                "house_id": channel.id,
                "img_url": fdfs.url(str(channel.index_image_url)),
                "title": channel.title
            })
            if (num >= 5):
                break

        return JsonResponse({
            "data":home_list,
            "errmsg": "OK",
            "errno": "0"
        })



class House_Details(View):
    #房屋详情页面
    def get(self,request,house_id):
        #数据库取出数据
        try:
            house = House.objects.get(id=house_id)
            comments = Order.objects.filter(house=house_id)
            house_img = HouseImage.objects.filter(house=house_id)
        except Exception as e:
            return JsonResponse({"errno":"4001",
                                 "errmsg":"数据库查询错误"})

        #数据库遍历出comments数据放入列表
        list1 = []
        for comment in comments:
            list1.append({
                'comment': comment.comment,
                'ctime': comment.create_time.strftime('%Y-%m-%d %H:%M:%S'),
                'user_name': comment.user.username
            })

        # 数据库遍历出facilities数据放入列表
        list2 = []
        facility_qs = house.facility.all()
        for facility in facility_qs:
            list2.append(facility.id)

        # 数据库遍历出img_urls数据放入列表
        list3 = []
        fdfs = FastDFSStorage()
        for img in house_img:
            list3.append(fdfs.url(str(img.url)))

        # 判断用户是否登录
        user_id = request.user.id if (request.user.is_authenticated) else  -1


        return JsonResponse(
            {'data':
                    {"house": {
                            "acreage": house.acreage,
                            "address": house.address,
                            "beds": house.beds,
                            "capacity":house.capacity,
                            "comments":list1,
                            "deposit":house.deposit,
                            "facilities":list2,
                            "hid":house_id,
                            "img_urls":list3,
                            "max_days":house.max_days,
                            "min_days":house.min_days,
                            "price":house.price,
                            "room_count":house.room_count,
                            "title":house.title,
                            "unit":house.unit,
                            "user_avatar":fdfs.url(str(house.user.avatar)),
                            "user_id":house.user.id,
                            "user_name":house.user.real_name
                            },
                    'user_id': user_id},
            'errmsg': 'ok',
            'errno': '0'})


